package com.example.administrator.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/3/21 0021.
 */
public class ListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Student> mList;
    private LayoutInflater inflater;
    public ListAdapter(Context context, List<Student> list) {
        mList = list;
        mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局加载成View
        View view = inflater.inflate(R.layout.list_item,null);
        //找到控件
        TextView tvName = (TextView) view.findViewById(R.id.tv_name);
        TextView tvAge = (TextView) view.findViewById(R.id.tv_age);
        TextView tvSex = (TextView) view.findViewById(R.id.tv_sex);
        TextView tvClass = (TextView) view.findViewById(R.id.tv_class);
        //赋值
        Student student = mList.get(position);
        tvName.setText(student.getName());
        tvAge.setText(student.getAge()+"");
        tvSex.setText(student.getSex());
        tvClass.setText(student.getClassName());
        return view;
    }
}
